import { useIntersectionObserver, useIntervalFn } from "@vueuse/core"
import { onMounted, ref } from "vue"

export function useLazy(fn: () => void) {
    const target = ref(null)
    onMounted(() => {
        const { stop } = useIntersectionObserver(target.value, ([{ isIntersecting }]) => {
            if (isIntersecting) {
                fn()
                stop()
            }
        })
    })
    return target
}

export function useCountDown() {
    const counter = ref(0)
    const { pause, resume } = useIntervalFn(() => {
        if (counter.value === 0) {
            pause()
        } else {
            counter.value--
        }
    }, 1000, { immediate: false })

    const start = (num: number) => {
        counter.value = num
        resume()
    }

    return { counter, start }
}